package com.example.firstdemo.ui.screens.pages.problem.problemList

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.SubcomposeAsyncImage
import com.example.firstdemo.Config
import com.example.firstdemo.R
import com.example.firstdemo.network.api.problem.data.ProblemItem
import com.example.firstdemo.ui.screens.pages.problem.problemList.data.LocalProblemListDataProvider
import com.example.firstdemo.ui.screens.pages.problem.problemList.data.ProblemListUIState
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.FontSize
import com.example.firstdemo.ui.theme.Space
import com.example.firstdemo.ui.theme.eColors

@Composable
fun ProblemListScreen(
    uiState: ProblemListUIState,
    padding: PaddingValues = PaddingValues(0.dp),
    onGetProblemList: () -> Unit = {},
    onGotoDetail: (passTime: String, uid: String, stationName: String) -> Unit = { _, _, _ -> }
) {
    val problemList = uiState.problemList

    LaunchedEffect(Unit) {
        onGetProblemList()
    }

    Surface(
        modifier = Modifier
            .padding(padding)
            .fillMaxSize()
    ) {
        if (problemList.isNotEmpty()) {
            Box {
                LazyColumn(
                    contentPadding = PaddingValues(Space.small),
                    verticalArrangement = Arrangement.spacedBy(Space.small)
                ) {
                    items(problemList.size) { index ->
                        ProblemListItem(
                            problemList[index],
                            onClick = { passTime, uid, stationName ->
                                onGotoDetail(passTime, uid, stationName)
                            })
                    }
                }
            }
        } else {
            Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Image(
                        painterResource(R.drawable.empty),
                        null,
                        modifier = Modifier.size(300.dp)
                    )
                    Text(
                        text = "暂无数据",
                        color = MaterialTheme.eColors.text.placeholder,
                        fontSize = FontSize.large,
                        fontWeight = FontWeight.W400,
                        modifier = Modifier.offset(y = (-90).dp)
                    )
                }
            }
        }
    }
}


@Composable
fun ProblemListItem(
    problem: ProblemItem,
    modifier: Modifier = Modifier,
    onClick: (passTime: String, uid: String, stationName: String) -> Unit = { _, _, _ -> }
) {
    Card(
        shape = MaterialTheme.shapes.medium,
        modifier = modifier.clickable(
            onClick = {
                onClick(problem.passTime, problem.uid, problem.vehicle.stationName ?: "")
            }
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 1.dp
        )
    ) {
        Box(Modifier.padding(Space.small)) {
            Row(Modifier.wrapContentHeight()) {
                Surface(
                    shape = MaterialTheme.shapes.medium,
                    modifier = Modifier
                        .width(110.dp)
                        .height(75.dp)
                ) {
                    SubcomposeAsyncImage(
                        model = "${Config.BASE_URL}${problem.imgPath.removePrefix("/")}",
//                        loading = {
//                            CircularProgressIndicator()
//                        },
                        error = {
                            Box(contentAlignment = Alignment.Center) {
                                Image(
                                    painter = painterResource(R.drawable.img_error),
                                    null,
                                    contentScale = ContentScale.Crop,
                                    modifier = Modifier.size(40.dp)
                                )
                            }
                        },
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize()
                    )
                }
                Spacer(Modifier.width(12.dp))
                Box(Modifier.weight(1f)) {
                    Column {
                        Text(problem.remarks, style = MaterialTheme.typography.titleMedium)
                        Text(
                            problem.vehicle.stationName ?: "",
                            style = MaterialTheme.typography.bodySmall
                        )
                        Text(problem.passTime, style = MaterialTheme.typography.bodySmall)
                        Text(problem.carriageNo, style = MaterialTheme.typography.bodySmall)
                    }
                }
            }
        }
    }
}

@Preview(
    showBackground = true,
)
@Composable
fun ProblemItemPreview() {
    AppTheme() {
        ProblemListItem(
            problem = LocalProblemListDataProvider.problem
        )
    }
}


@Preview(
    showBackground = true,
    showSystemUi = true
)
@Composable
fun ProblemListScreenPreview() {
    AppTheme() {
        ProblemListScreen(
            ProblemListUIState(
                problemList = LocalProblemListDataProvider.problemList
            )
        )
    }
}

